﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using CNCSoft.ErpApps.TMS.myclass;
using Common;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmBanSach : Form
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();
        CommonClass mine = new CommonClass();

        public frmBanSach()
        {
            InitializeComponent();
        }

        private void frmBanSach_Load(object sender, EventArgs e)
        {
            LoadControl();
        }


        private void searchLookUpEditInvoice_EditValueChanged(object sender, EventArgs e)
        {
            string invoice_number = searchLookUpEditInvoice.EditValue.ToString();
            if (invoice_number != "")
                GetData(invoice_number);
        }


        private void btnBanSach_Click(object sender, EventArgs e)
        {
            try
            {
                string invoice_number = searchLookUpEditInvoice.EditValue.ToString();
                string masach = searchLookUpEditSach.EditValue.ToString();
                InsertData(invoice_number, masach);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        private void LoadControl()
        {
            try
            {
                LoadInvoice();
                LoadSach();
                if (searchLookUpEditInvoice.EditValue != null)
                {
                    string invoice_number = searchLookUpEditInvoice.EditValue.ToString();
                    if (invoice_number != "")
                        GetData(invoice_number);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadInvoice()
        {
            try
            {
                if (ds.Tables["TableInvoice"] != null)
                    ds.Tables["TableInvoice"].Clear();
                string sql = @"SELECT inv.InvoiceNumber, enq.HoLot, enq.Ten FROM Invoice inv INNER JOIN Enquiry enq ON inv.EnquiryNumber = enq.EnquiryNumber";
                mine.ExecuteDataAdapter(sql, conn, ds, "TableInvoice", "InvoiceNumber", "Ten", searchLookUpEditInvoice);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadSach()
        {
            try
            {
                if (ds.Tables["TableSach"] != null)
                    ds.Tables["TableSach"].Clear();
                string sql = @"SELECT * FROM DM_SACH";
                mine.ExecuteDataAdapter(sql, conn, ds, "TableSach", "MaSach", "TenSach", searchLookUpEditSach);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void GetData(string invoice_number)
        {
            try
            {
                if (ds.Tables["TableInvoiceSach"] != null)
                    ds.Tables["TableInvoiceSach"].Clear();
                string sql = @"SELECT invsach.*, dmsach.MaSach, dmsach.TenSach, dmsach.GiaBan FROM 
                                Invoice_Sach invsach INNER JOIN DM_SACH dmsach 
                                ON invsach.MaSach = dmsach.MaSach
                                WHERE InvoiceNumber = '" + invoice_number + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoiceSach");
                
                gridControlInvoiceSach.DataSource = ds;
                gridControlInvoiceSach.DataMember = "TableInvoiceSach";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void InsertData(string invoicenumber, string masach)
        {
            try
            {
                int i = 0;
                string sql = @"INSERT INTO Invoice_Sach(InvoiceNumber, MaSach) VALUES(@invoicenumber, @masach)";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@invoicenumber", invoicenumber);
                cmd.Parameters.AddWithValue("@masach", masach);
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Thực hiện bán sách có mã số " + masach + " cho học sinh có mã số " + invoicenumber + " thành công");
                    GetData(invoicenumber);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
